package com.hr.classes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hr.classes.entity.TbAttendanceRule;
import com.hr.classes.entity.base.TableListDTO;
import com.hr.classes.entity.dto.TbAttendanceRuleDTO;
import com.hr.classes.entity.po.TbAttendanceRulePO;
import com.hr.classes.entity.vo.TbAttendanceRuleVO;
import com.hr.classes.mapper.TbAttendanceRuleMapper;
import com.hr.classes.service.TbAttendanceRuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <pre>
 * 描述：TODO
 * </pre>
 *
 * @类名：com.hr.classes.service.impl.TbAttendanceRuleServiceImpl
 * @创建日期: 2024/5/7 15:20
 */
@Service
public class TbAttendanceRuleServiceImpl implements TbAttendanceRuleService {

    @Autowired
    private TbAttendanceRuleMapper tbAttendanceRuleMapper;
    @Override
    public TableListDTO<TbAttendanceRuleDTO> getList(TbAttendanceRuleVO tbAttendanceRuleVO) {
        List<TbAttendanceRuleDTO> list = new ArrayList<>();

        LambdaQueryWrapper<TbAttendanceRule> queryWrapper = Wrappers.<TbAttendanceRule>lambdaQuery()
                .like(StringUtils.isNotBlank(tbAttendanceRuleVO.getName()), TbAttendanceRule::getName, tbAttendanceRuleVO.getName());
        Integer count = tbAttendanceRuleMapper.selectCount(queryWrapper);
        if (count > 0) {
            if (tbAttendanceRuleVO.getPageNum() != null && tbAttendanceRuleVO.getPageSize() != null) {
                PageHelper.startPage(tbAttendanceRuleVO.getPageNum(), tbAttendanceRuleVO.getPageSize());
            }
            List<TbAttendanceRule> tbAttendanceRules = tbAttendanceRuleMapper.selectList(queryWrapper);
            TbAttendanceRuleDTO tbAttendanceRuleDTO = null;
            for (TbAttendanceRule tbAttendanceRule : tbAttendanceRules) {
                tbAttendanceRuleDTO = new TbAttendanceRuleDTO();
                BeanUtils.copyProperties(tbAttendanceRule, tbAttendanceRuleDTO);

                if (tbAttendanceRuleDTO.getType().equals("固定时间")){
                    tbAttendanceRuleDTO.setFlexTime(null);
                }else if(tbAttendanceRuleDTO.getType().equals("弹性时间")){
                    tbAttendanceRuleDTO.setClockPeriod("");
                    tbAttendanceRuleDTO.setWorkUpTime(null);
                    tbAttendanceRuleDTO.setWorkDownTime(null);
                    tbAttendanceRuleDTO.setClockStartTime(null);
                    tbAttendanceRuleDTO.setClockEndTime(null);
                }
                list.add(tbAttendanceRuleDTO);
            }
        }

        PageInfo<TbAttendanceRuleDTO> pageInfo = new PageInfo<>(list);

        pageInfo.setTotal(count);

        return TableListDTO.setPageData(pageInfo);
    }

    @Override
    public Integer add(TbAttendanceRuleVO tbAttendanceRuleVO) {
        TbAttendanceRule tbAttendanceRule = new TbAttendanceRule();
        BeanUtils.copyProperties(tbAttendanceRuleVO, tbAttendanceRule);
        return tbAttendanceRuleMapper.insert(tbAttendanceRule);
    }

    @Override
    public TbAttendanceRuleDTO get(Integer id) {
        TbAttendanceRuleDTO tbAttendanceRuleDTO = new TbAttendanceRuleDTO();
        TbAttendanceRule tbAttendanceRule = tbAttendanceRuleMapper.selectById(id);
        if (tbAttendanceRule == null) {
            return tbAttendanceRuleDTO;
        }

        BeanUtils.copyProperties(tbAttendanceRule, tbAttendanceRuleDTO);
        return tbAttendanceRuleDTO;
    }

    @Override
    public Integer update(TbAttendanceRuleVO tbAttendanceRuleVO) {
        TbAttendanceRule tbAttendanceRule = new TbAttendanceRule();

        BeanUtils.copyProperties(tbAttendanceRuleVO, tbAttendanceRule);

        return tbAttendanceRuleMapper.updateById(tbAttendanceRule);
    }

    @Override
    public Integer deletes(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return 0;
        }
        return tbAttendanceRuleMapper.deleteBatchIds(ids);
    }

    @Override
    public List<TbAttendanceRulePO> getListPO(TbAttendanceRuleVO tbAttendanceRuleVO) {
        LambdaQueryWrapper<TbAttendanceRule> queryWrapper = Wrappers.<TbAttendanceRule>lambdaQuery()
                .like(StringUtils.isNotBlank(tbAttendanceRuleVO.getName()), TbAttendanceRule::getName, tbAttendanceRuleVO.getName())
                .in(!CollectionUtils.isEmpty(tbAttendanceRuleVO.getIds()), TbAttendanceRule::getId, tbAttendanceRuleVO.getIds());
        List<TbAttendanceRule> tbAttendanceRules = tbAttendanceRuleMapper.selectList(queryWrapper);

        if (CollectionUtils.isEmpty(tbAttendanceRules)) {
            return new ArrayList<>();
        }

        List<TbAttendanceRulePO> tbAttendanceRulePOS = new ArrayList<>();
        TbAttendanceRulePO tbAttendanceRulePO = null;

        Integer index = 0;
        for (TbAttendanceRule tbAttendanceRule : tbAttendanceRules) {
            index++;
            tbAttendanceRulePO = new TbAttendanceRulePO();
            BeanUtils.copyProperties(tbAttendanceRule, tbAttendanceRulePO);

            String status = "1".equals(tbAttendanceRule.getStatus()) ? "是" : "否";
            String clockTime = null;
            if (tbAttendanceRule.getClockStartTime() != null && tbAttendanceRule.getClockEndTime() != null) {
                clockTime = getDateStr(tbAttendanceRule.getClockStartTime()) + " - " + getDateStr(tbAttendanceRule.getClockEndTime());
            }
            String flexTime = null;
            if (tbAttendanceRule.getFlexTime() != null) {
                flexTime = tbAttendanceRule.getFlexTime().setScale(2, RoundingMode.HALF_UP).toPlainString();
            }

            tbAttendanceRulePO.setSno(index.toString());
            tbAttendanceRulePO.setWorkUpTime(getDateStr(tbAttendanceRule.getWorkUpTime()));
            tbAttendanceRulePO.setWorkDownTime(getDateStr(tbAttendanceRule.getWorkDownTime()));
            tbAttendanceRulePO.setStatus(status);
            tbAttendanceRulePO.setClockTime(clockTime);
            tbAttendanceRulePO.setFlexTime(flexTime);
            tbAttendanceRulePOS.add(tbAttendanceRulePO);
        }

        return tbAttendanceRulePOS;
    }

    private String getDateStr(Date date) {
        if (date == null) {
            return StringUtils.EMPTY;
        }

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
        return simpleDateFormat.format(date);
    }


}
